package com.xbongbong.pro.bi.commons.helper;

import com.google.common.hash.Hashing;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author gzt
 * @version 1.0
 * @Description -
 * @datetime - 2022/10/8-13:44
 * @since v1.0
 */
public class HashHelper {
    private static Map<String, Integer> partMap = new HashMap<String, Integer>();
    /**
     * @author  gzt
     * @Description 根据corpid,分区总数获取当前公司所在分区的分区号
     * @Date 13:46 2022/10/8
     * @param corpid
     * @param partCount
     * @return int
     * @throws
     **/

    public static int getPartNum(String corpid, int partCount) {
        Integer partNum = null;
        if(Objects.nonNull(corpid)){
            partNum = partMap.get(corpid);
            if (Objects.isNull(partNum)){
                partNum = Math.abs(Hashing.murmur3_32().hashString(corpid, StandardCharsets.UTF_8).asInt() % partCount);
                partMap.put(corpid,partNum);
            }
        }
        return partNum;
    }
}
